
package org.buildforce.uploader.metadata;

import java.io.File;

/**
 * @author mtodorov
 */
public class ArtifactMetaDataBuilder
{


    public ArtifactMetaData buildMetaData(String artifactPath)
    {
        ArtifactMetaData artifactMetaData = new ArtifactMetaData();
        artifactMetaData.setArtifactPathInRepository(artifactPath);
        artifactMetaData.setGroupId(parseGroupId(artifactPath));
        artifactMetaData.setArtifactId(parseArtifactId(artifactPath));
        artifactMetaData.setVersion(parseVersion(artifactPath));
        artifactMetaData.setClassifier(parseClassifier(artifactPath, artifactMetaData.getVersion()));
        artifactMetaData.setPackaging(parsePackaging(artifactPath));

        return artifactMetaData;
    }

    private String parseGroupId(String artifactPathInRepository)
    {
        // org/apache/velocity/velocity/1.5/velocity-1.5.jar
        // commons-collections/commons-collections/3.1.0/commons-collections-3.1.0.jar

        String[] s = artifactPathInRepository.split("/");
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < s.length - 3; i++)
        {
            sb.append(s[i]);

            // org/apache/wicket/wicket-spring/1.4.7-fredhopper-3-SNAPSHOT/wicket-spring-1.4.7-fredhopper-3-SNAPSHOT.jar
            //              4   /     3       /            2             /   1
            //        groupid  /    artifact /         version           / file name
            if (i < s.length - 4)
                sb.append(".");
        }

        String groupId = sb.toString();

        groupId = groupId.replaceAll("\\\\", "/");
        groupId = groupId.replaceAll("/", ".");

        return groupId;
    }

    private String parseArtifactId(String artifactPathInRepository)
    {
        String[] s = artifactPathInRepository.split("/");
        return s[s.length - 3];
    }

    private String parseVersion(String artifactPathInRepository)
    {
        String[] s = artifactPathInRepository.split("/");

        return s[s.length - 2];
    }

    private String parseClassifier(String artifactPathInRepository, String version)
    {
        // org/drools/drools-api/5.0.1-fredhopper-1/drools-api-5.0.1-fredhopper-1-tests.jar
        String[] s = artifactPathInRepository.split("/");

        String classifier = s[s.length - 1];

        try
        {
            String[] s1 = artifactPathInRepository.split(File.separator);
            String fileName = s1[s1.length-1];

            if (fileName.indexOf("SNAPSHOT") != -1 | version.indexOf("SNAPSHOT") == -1)
            {
                classifier = classifier.substring(classifier.indexOf(version) + version.length() +1,
                                                  classifier.lastIndexOf("."));
            }
            else
            {
                // Parse the fixed part of the version, by stripping out the snapshot part
                String fixedVersion = version.substring(0, version.indexOf("-SNAPSHOT"));

                classifier = classifier.substring(classifier.indexOf(fixedVersion) + fixedVersion.length() +1,
                                                  classifier.lastIndexOf("."));
            }
        }
        catch (IndexOutOfBoundsException e)
        {
            classifier = "";
        }

        return classifier;
    }

    private String parsePackaging(String artifactPathInRepository)
    {
        if (artifactPathInRepository.indexOf("pom.xml") != -1)
            return "pom";

        String[] s = artifactPathInRepository.split("/");
        String name = s[s.length - 1];

        return name.substring(name.lastIndexOf(".") + 1, name.length());
    }

}
